/*
** elevation.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:39:40 2011 michael guidet
** Last update Mon Jul 11 10:14:42 2011 michael guidet
*/

#include "server.h"

t_lvlup_req	lvlup[8] =
  {
    {1, 2, 1, {0, 1, 0, 0, 0, 0, 0}},
    {2, 3, 2, {0, 1, 1, 1, 0, 0, 0}},
    {3, 4, 2, {0, 2, 0, 1, 0, 2, 0}},
    {4, 5, 4, {0, 1, 1, 2, 0, 1, 0}},
    {5, 6, 4, {0, 1, 2, 1, 3, 0, 0}},
    {6, 7, 6, {0, 1, 2, 3, 0, 1, 0}},
    {7, 8, 6, {0, 2, 2, 2, 2, 2, 1}},
    {0, 0, 0, {0, 0, 0, 0, 0, 0, 0}}
  };

int		action_elevation_check(t_fd *fd, t_env *e)
{
  t_player_list	*tmp;
  int		i;
  int		j;
  int		success;
  int		nb_player;

  success = 1;
  for (i = 0; lvlup[i].lvlrequired != fd->pl->level; i++);
  nb_player = 0;
  for (tmp = e->map[fd->pl->y][fd->pl->x].players; tmp; tmp = tmp->next)
    {
      if (tmp->drone->level != lvlup[i].lvlrequired)
	success = 0;
      nb_player++;
    }
  if (nb_player != lvlup[i].nb_player)
    success = 0;
  for (j = 0; j < ALL_RC; j++)
    if (j != NOURRITURE && e->map[fd->pl->y][fd->pl->x].ressource[j]
	!= lvlup[i].ressource[j])
      success = 0;
  return (success);
}

void		graph_elevation(t_env *e, int x, int y, int success)
{
  t_fd		*tmp;
  t_player_list	*tmp2;
  int		len;
  char		*cmd;
  char		cmd2[BUF_SIZE];

  len = BUF_SIZE;
  cmd = x_null(NULL, malloc(sizeof(*cmd) * (len + 1)), "malloc");
  sprintf(cmd2, "pie %d %d %d\n", x, y, success);
  xstrcat(&cmd, cmd2, &len);
  for (tmp2 = e->map[y][x].players; tmp2; tmp2 = tmp2->next)
    {
      sprintf(cmd2, "plv %d %d\n", tmp2->drone->id, tmp2->drone->level);
      xstrcat(&cmd, cmd2, &len);
    }
  sprintf(cmd2, "bct %d %d %d %d %d %d %d %d %d\n", x, y,
	  e->map[y][x].ressource[NOURRITURE], e->map[y][x].ressource[LINEMATE],
	  e->map[y][x].ressource[DERAUMERE], e->map[y][x].ressource[SIBUR],
	  e->map[y][x].ressource[MENDIANE], e->map[y][x].ressource[PHIRAS],
	  e->map[y][x].ressource[THYSTAME]);
  xstrcat(&cmd, cmd2, &len);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd);
  free(cmd);
}

void		action_elevation(t_fd *fd, t_env *e, char *cmd)
{
  t_player_list	*tmp;
  t_fd		*tmp2;
  int		success;
  char		cmd2[BUF_SIZE];

  fd->idaction = fd->wait = -1;
  if ((success = action_elevation_check(fd, e)))
    {
      check_endgame(fd, e, cmd);
      move_ressources(e, fd->pl->y, fd->pl->x);
      for (tmp = e->map[fd->pl->y][fd->pl->x].players;
	   tmp; tmp = tmp->next)
	tmp->drone->level++;
      sprintf(cmd2, "niveau actuel : %d\n", fd->pl->level);
    }
  else
    sprintf(cmd2, "ko\n");
  for (tmp = e->map[fd->pl->y][fd->pl->x].players; tmp; tmp = tmp->next)
    for (tmp2 = e->fd; tmp2; tmp2 = tmp2->next)
      if (tmp->drone == tmp2->pl)
	{
	  add_string_to_list(&tmp2->send_list, cmd2);
	  break;
	}
  graph_elevation(e, fd->pl->x, fd->pl->y, success);
}

void		graph_incantation(t_env *e, t_fd *fd)
{
  t_fd		*tmp2;
  t_player_list	*tmp;
  int		len;
  char		*cmd2;
  char		nb[24];

  len = BUF_SIZE;
  cmd2 = x_null(NULL, malloc(sizeof(*cmd2) * (len + 1)), "malloc");
  sprintf(cmd2, "pic %d %d %d %d", fd->pl->x, fd->pl->y,
	  fd->pl->level, fd->pl->id);
  for (tmp = e->map[fd->pl->y][fd->pl->x].players; tmp; tmp = tmp->next)
    {
      sprintf(nb, " %d", tmp->drone->id);
      xstrcat(&cmd2, nb, &len);
    }
  xstrcat(&cmd2, "\n", &len);
  for (tmp2 = e->fd; tmp2; tmp2 = tmp2->next)
    if (tmp2->type == GRAPH)
      add_string_to_list(&tmp2->send_list, cmd2);
  free(cmd2);
}

void		action_incantation(t_fd *fd, t_env *e, char *cmd)
{
  t_player_list	*tmp;
  t_fd		*tmp2;
  char		cmd2[BUF_SIZE];

  printf("%sDrone %d %s Execute %s%s\n", PURPLE, fd->pl->id,
	 fd->pl->team, cmd, DEFAUT);
  fd->wait = (long)(300.0f / (float)e->t * 1000000.0f);
  fd->idaction = 10;
  if (action_elevation_check(fd, e))
    sprintf(cmd2, "elevation en cours\n");
  else
    sprintf(cmd2, "ko\n");
  for (tmp = e->map[fd->pl->y][fd->pl->x].players; tmp; tmp = tmp->next)
    for (tmp2 = e->fd; tmp2; tmp2 = tmp2->next)
      if (tmp->drone == tmp2->pl)
	{
	  add_string_to_list(&tmp2->send_list, cmd2);
	  break;
	}
  graph_incantation(e, fd);
}
